package com.dddd.blog.bloguser.controller;


import com.dddd.blog.api.user.feign.SpiderFeignClient;
import com.dddd.blog.api.user.pojo.User;
import com.dddd.blog.blogcommon.annotation.ResponseResult;
import com.dddd.blog.blogcommon.constant.ResultCode;
import com.dddd.blog.blogcommon.controller.BaseController;
import com.dddd.blog.blogcommon.pojo.Result;
import com.dddd.blog.blogcommon.pojo.vo.PageQueryVO;
import com.dddd.blog.blogcommon.pojo.vo.UserListVO;
import com.dddd.blog.blogcommon.pojo.vo.UserVO;
import com.dddd.blog.blogcommon.utils.ObjectInvertUtils;
import com.dddd.blog.blogcommon.utils.ResultUtils;
import com.dddd.blog.bloguser.service.IUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dddd
 * @since 2022-02-27
 */
@RestController
@RequestMapping("/user")
@Api(description = "用户基本信息操作API接口")
@CrossOrigin
@ResponseResult
public class UserController extends BaseController {

    @GetMapping("/test")
    @ApiOperation(value = "用户信息的测试方法")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "请求api成功"),
                           @ApiResponse(code = 400, message = "找不到api")})
    public String test(){
        return "test";
    }

    @PostMapping("/test1")
    @ApiOperation(value = "用户信息的测试方法")
    public String test1(@ApiParam(name = "authorId", value="作者id",required = true)String authorId,
                        @ApiParam(name = "followId", value = "关注者id" ,required = true) String followId ){
        return "test";
    }

    /** ********************************************************************************/

    /**
     * 用户web层依赖
     */
    @Autowired
    private IUserService userService;


    @Resource
    private SpiderFeignClient spiderFeignClient;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取用户信息")
    public Result<UserVO> getUserById(@ApiParam("id") @PathVariable("id")Long id)
    {
            if(id == null){
                return ResultUtils.error(ResultCode.PARAMS_BLANK);
            }
        User user = userService.getUserById(id);
        UserVO rtObj = ObjectInvertUtils.invert(user,UserVO.class);

        return ResultUtils.success(rtObj);
    }


    // Spider <--> User

    /**
     * 从Spider服务加载用户昵称并存入User服务中
     * @return
     */
    @GetMapping("/loadNames")
    @ApiOperation(value = "从Spider中获取昵称列表")
    public Result loadUserNameFromSpider(){

        Result<List<String>> result = spiderFeignClient.loadAuthor();

        return result;
    }


    /**
     * 从Spider服务加载用户信息并存入User服务中
     * @return
     */
    @GetMapping("/loadUser")
    @ApiOperation(value = "从Spider中获取用户列表")
    public Result loadUserFromSpider(){

        // 获取用户列表
        Result<List<Map<String, Object>>> result = spiderFeignClient.loadAuthorList();

        List<Map<String, Object>> data = result.getData();

        // 循环单次插入数据
        data.forEach(item -> userService.addUserWithAuthor(item));

        return result;

    }

    /**
     * 从Spider服务分页加载用户信息并存入User服务中
     * @return
     */
    @PostMapping("/loadUser/page")
    @ApiOperation(value = "从Spider中获取用户列表")
    public Result loadUserFromSpiderPage(@ApiParam("页码") Long currentPage,
                                         @ApiParam("页面条数") Long pageSize){

        Result<List<Map<String, Object>>> result = spiderFeignClient.loadAuthorListPage(currentPage, pageSize);
        // TODO
        return result;
    }


    /**
     *
     * @return
     */
    @GetMapping("/count")
    @ApiOperation(value = "获取系统的用户数目")
    public Result getUserCount(){
        Integer count = userService.getUserCount();
        return ResultUtils.success(count);
    }

    /**
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取用户列表")
    public Result getUserList(){

        Map<String,Object> rtValue = new HashMap();

        List<UserListVO> userList = userService.getUserList();
        return ResultUtils.success(userList);
    }

    /**
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "获取用户列表")
    public Result getUserListPage(@RequestBody PageQueryVO vo){

        if(vo==null){
            return ResultUtils.error(ResultCode.PARAMS_BLANK);
        }

        Map<String,Object> result = new HashMap(4);
        Integer total = userService.getUserCount();
        List<UserListVO> list = userService.getUserList(vo.getCurrentPage(), vo.getPageSize());
        result.put("total",total);
        result.put("pages",total/vo.getPageSize());
        result.put("currentPage",vo.getCurrentPage());
        result.put("pageSize",vo.getPageSize());
        result.put("list",list);

        return ResultUtils.success(result);
    }


    @GetMapping("/getId/{name}")
    @ApiOperation(value = "根据用户名获取id")
    public Result getUserIdByName(@PathVariable("name")String name){
        User user = userService.getUserByName(name);
        return ResultUtils.success(user.getId());
    }

}

